package com.fafu.rbac.security.controller;

import com.fafu.common.redis.utils.ByteRedisUtil;
import com.fafu.common.web.constant.GlobalConst;
import com.fafu.rbac.security.bean.WegoUserDetails;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class LogoutController {
    @Resource
    private ByteRedisUtil<WegoUserDetails> redisUtil;

    @GetMapping("/logout")
    public String logout(){
        //获取SecurityContextHolder中的用户id
        UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        WegoUserDetails userDatails = (WegoUserDetails) authentication.getPrincipal();
        Long userId = userDatails.getUser().getId();
        //从Redis缓存中删除指定id的用户
        redisUtil.del("user:"+userId);
        return "{\"code\":200,\"msg\":\"注销成功！\"}";
    }
}
